package com.jinyuanchun.learnjava;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Main1 {

    public static void main(String[] args) {
	// write your code here
        Car myCar1 = new Car("tom","red");
        Map<String,Object> tmp_hash = object2hash(myCar1);
        for(String key:tmp_hash.keySet()){
            System.out.println("key : "+key);
            System.out.println("\t value : "+tmp_hash.get(key));
        }
    }

    private static Map<String, Object> object2hash(Car myCar1) {
        Map<String,Object> result = new HashMap<>();
        /**
         * 返回 Class 对象所表示的类或接口所声明的所有字段，
         * 包括公共、保护、默认（包）访问和私有字段，但不包括继承的字段。
         */
        Field[] fields=Car.class.getDeclaredFields();
        System.out.println("Test类里面的所有字段属性的个数为："+fields.length+"个，分别为：");


        for(int i=0;i<fields.length;i++){
            String attributeName=fields[i].getName();
            System.out.println(attributeName);
            attributeName = attributeName.toLowerCase();  //将key置为小写，默认为对象的属性
            try {
                // 获取原来的访问控制权限
                boolean accessFlag = fields[i].isAccessible();
                // 修改访问控制权限
                fields[i].setAccessible(true);
                // 获取在对象f中属性fields[i]对应的对象中的变量
                Object o = fields[i].get(myCar1);
                if (o != null)
                    result.put(attributeName, o);
                // System.out.println("传入的对象中包含一个如下的变量：" + varName + " = " + o);
                // 恢复访问控制权限
                    fields[i].setAccessible(accessFlag);
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
        }
        return result;
    }
}

class Car {
    private String name;
    private String color;

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

class Person {
    private String name;
    private Integer age;
    private Integer telphone;
    private String email;

    public Person(String name, Integer age, Integer telphone, String email) {
        this.name = name;
        this.age = age;
        this.telphone = telphone;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getTelphone() {
        return telphone;
    }

    public void setTelphone(Integer telphone) {
        this.telphone = telphone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
